<?php
/**
 * 自定义菜单
 *
 */
defined('In33hao') or exit('Access Invalid!');

class diymenuModel extends Model
{

    public function __construct()
    {
        parent::__construct('diymenu');
    }

    /**
     * 类别列表
     *
     * @param array $condition 检索条件
     * @return array 数组结构的返回结果
     */
    public function getDiymenuList($condition, $order = 'id asc', $limit = '')
    {
        $result = $this->field('*')->where($condition)->order($order)->limit($limit)->select();

        return $result;
    }

    /**
     * 总数
     * @param array $condition 检索条件
     * @return array 数组结构的返回结果
     */
    public function getDiymenuCount($condition, $order = 'id asc', $limit = '')
    {
        $result = $this->where($condition)->order($order)->limit($limit)->count();

        return $result;
    }


    /**
     * 取分类列表，按照深度归类
     *
     * @param array $condition 检索条件
     * @param int $show_deep 显示深度
     * @return array 数组类型的返回结果
     */
    public function getTreeClassList($condition, $show_deep = '2')
    {
        $class_list = $this->getDiymenuList($condition);
        /*
        $show_deep = intval($show_deep);
$result = array();
if(is_array($class_list) && !empty($class_list)) {

    $result = $this->_getTreeClassList($show_deep,$class_list);

}
         * */
        $result = $this->childTree($class_list, 0);
        return $result;
    }


    public function childTree($res, $myid, $deep = 1)
    {
        static $childs = array();
        foreach ($res as $k => $v) {
            if ($v['pid'] == $myid) {
                $v['deep'] = $deep;
                $childs[] = $v;
                $this->childTree($res, $v['id'], $v['deep'] + 1);
            }
        }
        return $childs;
    }

    public function checkLevelOne()
    {
        $res = $this->field('count(id) as num')->where(array('pid' => 0))->select();
        if ($res[0]['num'] >= 3) {
            return false;
        } else {
            return true;
        }
    }

    public function checkLevelTwo($pid)
    {
        $res = $this->field('count(id) as num')->where(array('pid' => $pid))->select();
        if ($res[0]['num'] >= 5) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * 递归 整理分类
     *
     * @param int $show_deep 显示深度
     * @param array $class_list 类别内容集合
     * @param int $deep 深度
     * @param int $parent_id 父类编号
     * @param int $i 上次循环编号
     * @return array $show_class 返回数组形式的查询结果
     */
    private function _getTreeClassList($show_deep, $class_list, $deep = 1, $parent_id = 0, $i = 0)
    {
        static $show_menu = array();//树状的平行数组
        if (is_array($class_list) && !empty($class_list)) {
            $size = count($class_list);
            if ($i == 0) $show_menu = array();//从0开始时清空数组，防止多次调用后出现重复
            for ($i; $i < $size; $i++) {//$i为上次循环到的分类编号，避免重新从第一条开始
                $val = $class_list[$i];
                $stc_id = $val['id'];
                $stc_parent_id = $val['pid'];
                if ($stc_parent_id == $parent_id) {
                    $val['deep'] = $deep;
                    $show_menu[] = $val;
                    if ($deep < $show_deep && $deep < 2) {//本次深度小于显示深度时执行，避免取出的数据无用
                        $this->_getTreeClassList($show_deep, $class_list, $deep + 1, $stc_id, $i + 1);
                    }
                }
                if ($stc_parent_id > $parent_id) break;//当前分类的父编号大于本次递归的时退出循环
            }
        }
        return $show_menu;
    }


    /**
     * 单条详细信息（查库）
     * @param array $condition
     * @param string $field
     * @return array
     */
    public function getDiymenuInfo($condition, $field = '*', $master = false)
    {
        return $this->table('diymenu')->field($field)->where($condition)->master($master)->find();
    }

    /**
     * 新增
     *
     * @param array $insert 数据
     * @param string $table 表名
     */
    public function addDiymenu($insert)
    {
        return $this->table('diymenu')->insert($insert);
    }

    /**
     * 编辑菜单
     * @param array $condition
     * @param array $data
     */
    public function editDiymenu($condition, $data)
    {
        return $this->table('diymenu')->where($condition)->update($data);
    }

    /**
     * 删除
     * @param int $id 记录ID
     * @return bool 布尔类型的返回结果
     */
    public function del($id)
    {
        if (intval($id) > 0) {
            $res = $this->field('id')->where(array('pid' => $id))->select();
            if ($res) {
                foreach ($res as $v) {
                    $arr[] = $v['id'];
                }
                $ids = implode(',', $arr) . ',' . $id;
            } else {
                $ids = $id;
            }
            $where['id'] = array('in', $ids);
            $result = $this->where($where)->delete();
            return $result;
        } else {
            return false;
        }
    }

    public function delcheck($ids)
    {
        if ($ids) {
            $where['id'] = array('in', $ids);
            $result = $this->where($where)->delete();
            return $result;
        } else {
            return false;
        }
    }
}